package dao.impl;

import dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    List<User> users = new ArrayList<>();
    @Override
    public List<User> findAll() {
        //引入mybaties配置文件
        String resource = "mybatis-config.xml";
        SqlSession sqlsession = null;
        //加载配置文件
        try {
            InputStream inputStream =Resources.getResourceAsStream(resource);
            //创建会话工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //创建会话
            sqlsession =sqlSessionFactory.openSession();
            //创建执行
            List<User> users = sqlsession.getMapper(UserDao.class).findAll() ;
            //返回查询结果
            return users;


        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            sqlsession.close();
        }
    }
}
